package com.rich.dmr.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Pattern;

public class ConvertUtils {
    private static final  byte[] hex = "0123456789ABCDEF".getBytes();
    private static final String str = "0123456789ABCDEF";
    private ConvertUtils() {}

    private static class TypeConversionUtilsHolder{
        private static ConvertUtils mTypeConversionUtils = new ConvertUtils();
    }

    public static ConvertUtils getInstance() {
        return TypeConversionUtilsHolder.mTypeConversionUtils;
    }


    public int checkSum(byte[] buf, int len) {
        int sum = 0 ,i = 0,temp , temp_hi,temp_lo;
        while (len > 1) {
            temp_hi = buf[i];
            temp_lo = buf[i + 1];
            if (temp_hi < 0) {
                temp_hi += 256;
            }
            if (temp_lo < 0) {
                temp_lo += 256;
            }
            temp = (temp_hi << 8) | temp_lo;
            sum += 0xFFFF & temp;
            i += 2;
            len -= 2;
        }
        if (len > 0) {
            sum += (0xFF & buf[i]) << 8;
        }
        while ((sum >> 16) > 0) {
            sum = (sum & 0xFFFF) + (sum >> 16);
        }
        int size =  buf.length;
        if(size > 6){
            buf[4] = (byte) (((sum ^ 0xFFFF) & 0xff00) >> 8);
            buf[5] = (byte) ((sum ^ 0xFFFF) & 0xff);
        }

        return (sum ^ 0xFFFF);
    }


    public static String str2HexStr(String str)
    {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++)
        {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }


    public static String int2HexString(int contact) {
        byte[] contacts = new byte[4];
        contacts[0] = (byte) (contact & 0xFF);
        contacts[1] = (byte) ((contact & 0xFF00) >> 8);
        contacts[2] = (byte) ((contact & 0xFF0000) >> 16);
        contacts[3] = (byte) ((contact & 0xFF000000) >> 24);
        return bytes2HexString(contacts);
    }

    public static String short2HexString(int length) {
        byte[] contacts = new byte[2];
        contacts[0] = (byte) ((length & 0xFF00) >> 8);
        contacts[1] = (byte) (length & 0xFF);
        return bytes2HexString(contacts);
    }

    public static String byte2Small(byte[] data) {
        byte[] dataResponse = new byte[data.length];
        for (int i=0; i<data.length;i++){
            dataResponse[i] = data[data.length - i - 1];
        }
        return bytes2HexString(dataResponse);
    }
    public static String hexStr2Str(String hexStr) {

        char[] hex = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;

        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hex[2 * i]) * 16;
            n += str.indexOf(hex[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        String src =null;
        try {
            src = new String(bytes, "GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return src;
    }

    public static String bytes2HexString(byte[] b) {
        byte[] buff = new byte[2 * b.length];
        for (int i = 0; i < b.length; i++) {
            buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
            buff[2 * i + 1] = hex[b[i] & 0x0f];
        }
        return new String(buff);
    }

    public static byte[] hexStr2Bytes(String src){
        src = src.trim().replace(" ", "").toUpperCase(Locale.US);
        int m,n;
        int iLen=src.length()/2;
        byte[] ret = new byte[iLen];

        for (int i = 0; i < iLen; i++){
            m=i*2+1;
            n=m+1;
            ret[i] = (byte)(Integer.decode("0x"+ src.substring(i*2, m) +
                    src.substring(m,n)) & 0xFF);
        }
        return ret;
    }

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]+)?$");
        return pattern.matcher(str).matches();
    }


    public static BigDecimal div(int v1, int v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, 4, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal div(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, 4, BigDecimal.ROUND_HALF_UP);
    }

    public static int getRandColorCode(){
        String r,g,b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(156)+80).toUpperCase();
        g = Integer.toHexString(random.nextInt(156)+80).toUpperCase();
        b = Integer.toHexString(random.nextInt(156)+80).toUpperCase();
        //b = Integer.toHexString(random.).toUpperCase();
        r = r.length()==1 ? "0" + r : r ;
        g = g.length()==1 ? "0" + g : g ;
        b = b.length()==1 ? "0" + b : b ;

        String hexString = "ff"+r+g+b;
        BigInteger bi = new BigInteger(hexString,16);
        return bi.intValue();
    }
}
